package biz.datalk.industrialland.admin;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;

/**
 * @author tarofang@163.com
 */
@Slf4j
@EnableScheduling
@EnableAsync
@EnableTransactionManagement
@SpringBootApplication(scanBasePackages = {"biz.datalk.industrialland", "biz.datalk.upload"})
public class IndustrialLandCoreApplication {

    public static void main(String[] args) throws UnknownHostException {
        log.info(" =======================================================> 启动开始");
        ConfigurableApplicationContext run = SpringApplication.run(IndustrialLandCoreApplication.class, args);
        printApplicationUrl(run);
        // 设置Spring环境下全局默认的Locale配置
        LocaleContextHolder.setDefaultLocale(Locale.CHINESE);
        log.info(" =======================================================> 启动完成");
    } ///~

    private static void printApplicationUrl(ConfigurableApplicationContext application) {
        Environment env = application.getEnvironment();
        String ip = env.getProperty("server.address", "");
        try {
            if (StringUtils.isBlank(ip)) {
                ip = InetAddress.getLocalHost().getHostAddress();
            }
        } catch (Exception ex) {
            ip = "127.0.0.1";
        }

        String port = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        contextPath = StringUtils.trimToEmpty(contextPath);
        contextPath = StringUtils.equals("/", contextPath) ? StringUtils.EMPTY : contextPath;
        // @formatter:off
        log.info("\n----------------------------------------------------------\n\t"
                + "Application is running! Access URLs:\n\t"
                + "\thttp://" + ip + ":" + port + contextPath + "\n"
                + "----------------------------------------------------------");
        // @formatter:on
    }
}
